//
//  SelectItem.m
//  TycoonSale
//
//  Created by molon on 15/7/29.
//  Copyright (c) 2015年 molon. All rights reserved.
//

#import "SelectItem.h"
#import <MLKit.h>

@implementation SelectItem

@end

@implementation SelectItemArray


+ (instancetype)selectItemsWithNames:(NSArray*)names
{
    SelectItemArray *selectItems = [SelectItemArray new];
    for (NSInteger i=0; i<names.count; i++) {
        SelectItem *item = [SelectItem new];
        item.name = names[i];
        item.ID = SIMPLE_INTEGER_STRING(i);
        [selectItems addObject:item];
    }
    
    CHILD(SelectItem,[selectItems firstObject]).selected = YES;
    return selectItems;
}

- (void)singleSelectWithItem:(SelectItem*)item
{
    if (!item||![self containsObject:item]) {
        return;
    }
    for (SelectItem *selectItem in self) {
        selectItem.selected = [selectItem isEqual:item];
    }
}

- (SelectItem*)singleSelectWithItemID:(NSString*)ID
{
    if(!ID||![[self arrayWithElementsOfModelObjectForElementKey:@"ID"] containsObject:ID]){
        return nil;
    }
    
    SelectItem *seletedItem = nil;
    for (SelectItem *item in self) {
        item.selected = [item.ID isEqualToString:ID];
        seletedItem = item.selected?item:nil;
    }
    return seletedItem;
}


- (NSInteger)singleSelectedItemIndex
{
    for (NSInteger i=0; i<self.count; i++) {
        if (CHILD(SelectItem, self[i]).selected) {
            return i;
        }
    }
    return NSNotFound;
}

- (SelectItem*)singleSelectedItem
{
    NSInteger index = [self singleSelectedItemIndex];
    if (index==NSNotFound) {
        return nil;
    }
    
    return self[index];
}


- (void)multiSelectWithIDs:(NSArray*)ids
{
    for (SelectItem *item in self) {
        item.selected = ([ids containsObject:item.ID]);
    }
}

- (void)multiSelectAll
{
    for (SelectItem *item in self) {
        item.selected = YES;
    }
}

- (void)multiUnselectAll
{
    for (SelectItem *item in self) {
        item.selected = NO;
    }
}

- (SelectItemArray*)multiSelectedItems
{
    SelectItemArray *arr = [SelectItemArray new];
    for (SelectItem *item in self) {
        if (item.selected) {
            [arr addObject:item];
        }
    }
    return arr;
}

- (NSArray*)multiSelectedItemIDs
{
    return [[self multiSelectedItems]arrayWithElementsOfModelObjectForElementKey:@"ID" isIgnoreNil:YES];
}

@end

@implementation RegionSelectItem


@end

@implementation RegionSelectItemArray


@end